﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using Microsoft.Practices.Web.UI.WebControls;
using TaskKernel.Domain;
using TaskWebApplicationUI.PresenterLayer;

namespace TaskWebApplicationUI
{
    public interface IAdminDepartmentsView : IAbstractView
    {
        List<Department> Departments { set; }
        string Message { set; }
    }

    public partial class AdminDepartments : ViewPage<IAdminDepartmentsView, AdminDepartmentsPresenter>,
                                            IAdminDepartmentsView
    {
        #region IAdminDepartmentsView Members

        public List<Department> Departments
        {
            set
            {
                Department element = value.First();
                ObjectContainerDataSource1.DataObjectTypeName = element.GetType().AssemblyQualifiedName;
                ObjectContainerDataSource1.DataSource = value;
            }
        }

        public string Message
        {
            set { Label1.Text = value; }
        }

        public string DepartmentName
        {
            set { throw new NotImplementedException(); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Presenter.OnViewInitialized();
        }

        protected void ObjectContainerDataSource1_Deleted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var delDepartment = (Department) e.Instance;
            try
            {
                Presenter.OnDeleteDepartment(delDepartment);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }

        protected void ObjectContainerDataSource1_Updated(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var delDepartment = (Department) e.Instance;
            try
            {
                Presenter.OnUpdateDepartment(delDepartment);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }

        protected void ObjectContainerDataSource1_Inserted(object sender, ObjectContainerDataSourceStatusEventArgs e)
        {
            var delDepartment = (Department) e.Instance;
            try
            {
                Presenter.OnAddDepartment(delDepartment);
            }
            catch (SecurityException)
            {
                Message = "Обратитесь к администратору.";
                Presenter.OnViewInitialized();
            }
        }
    }
}